package com.example.administrator.seniorplayer.utilis;

import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

import java.io.File;

/**
 * 作者："徐继丰" on 2015/10/13 0013 22:13
 * <p/>
 * 邮箱：1072666163@qq.com
 * 用于视频播放
 */
public class VideoViewPlayer extends Activity {
    public static VideoUri uri;
    // private String videoPath;
    private VideoView videoViewPlayer;
    // private int sign;
    private boolean flag = true;
    private float first_y;
    private float last_y;
    private float first_x;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        videoViewPlayer = new VideoView(this);
        //自带的播放控制器
        videoViewPlayer.setMediaController(new MediaController(this));
        setContentView(videoViewPlayer);
       /* Bundle vPath = getIntent().getExtras();
        sign = vPath.getInt("PlayerSign");*/
        /*if (sign == 1) {
            videoPath = vPath.getString("path");
            videoPathPlayer(videoPath);
        } else {*/
        Uri uri1 = Uri.parse("http://huiyi.qeebu.cn/teamin/Public/Uploads/information/5576b833ac235.mp4");
        videoUriPlayer(uri1);
           /* if (flag == false) {
                new Thread(
                        new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    Thread.sleep(3000);
                                    finish();
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                ).start();
            }*/
        //}

        WindowManager wm = this.getWindowManager();
        final int width = wm.getDefaultDisplay().getWidth();

        videoViewPlayer.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        first_x = event.getX();
                        first_y = event.getY();
                        break;
                    case MotionEvent.ACTION_UP:
                        last_y = event.getY();
                        if (first_x <= width / 2) {
                            //BrightnessTools light = new BrightnessTools();
                            if (last_y < first_y) {
                                //TODO 加大亮度
                                Brightness.stopAutoBrightness(VideoViewPlayer.this);
                                int lin = Brightness.getScreenBrightness(VideoViewPlayer.this);
                                if (lin != 255) {
                                    lin = lin + 8;
                                    Brightness.setBrightness(VideoViewPlayer.this, lin);
                                    Brightness.saveBrightness(getContentResolver(), lin);
                                }
                            } else {
                                //TODO 減小亮度
                                Brightness.stopAutoBrightness(VideoViewPlayer.this);
                                int lin = Brightness.getScreenBrightness(VideoViewPlayer.this);
                                lin = lin - 8;
                                if (lin > 0) {
                                    Brightness.setBrightness(VideoViewPlayer.this, lin);
                                    Brightness.saveBrightness(getContentResolver(), lin);
                                }
                            }
                        } else {
                            if (last_y < first_y) {
                                //TODO 加大音量
                                //音量控制,初始化定义
                                AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
                                //最大音量
                                int maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
                                //当前音量
                                int currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
                                currentVolume = currentVolume + 1;
                                if (currentVolume < maxVolume) {
                                    mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume, 0);
                                }

                            } else {
                                //TODO 減小音量
                                //音量控制,初始化定义
                                AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
                                //最大音量
                                int maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
                                //当前音量
                                int currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
                                currentVolume = currentVolume - 1;
                                if (currentVolume >= 0) {
                                    mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume, 0);
                                }
                            }
                        }


                        break;
                }
                return true;
            }
        });
    }

    public void videoUriPlayer(final Uri videoUri) {

        if (videoUri != null) {
            videoViewPlayer.setVideoURI(videoUri);
            videoViewPlayer.start();
        }

    }

    public void videoPathPlayer(String videoPath) {
        File file = new File(videoPath);
        if (file.exists()) {
            videoViewPlayer.setVideoPath(videoPath);
            videoViewPlayer.start();
        } else {
            Toast.makeText(VideoViewPlayer.this, "文件路径出错啦!", Toast.LENGTH_SHORT).show();
        }
    }


}
